//index.js
//获取应用实例
const app = getApp()
const api = require('../../utils/api.js');
Page({
	data: {
		phone: "",
		checkCode: "",
		smsTrue: false,
		countdown: 60,
		smsName: "获取验证码",
	},
	//事件处理函数
	login: function() {
		let _this = this;
		//缓存商户id
		wx.setStorage({
		  	key:"customerId",
		  	data:"3"
		})
		//待核销订单
//		wx.switchTab({
//			url: '../cancel/list'
//		})
		api.loginByPhone({
			data: {
				phone:this.data.phone,
				veriCode:this.data.checkCode
			},
			success: function(res) {
				if(res.data.code == 0) {
					//缓存商户id
					wx.setStorage({
					  	key:"customerId",
					  	data:res.data.id
					})
					//待核销订单
					wx.switchTab({
						url: '../cancel/list'
					})
				}else{
					wx.showModal({
						title: '提示',
						content: res.data.msg,
						showCancel: false,
						success: function(res) {
		
						}
					})
				}
			},
		});
		
	},
	sendCode: function() {
		console.log("###############" + this.data.phone);
		if(this.data.phone == "") {
			wx.showModal({
				title: '提示',
				content: "请输入手机号",
				showCancel: false,
				success: function(res) {

				}
			})
			return false;
		} else {
			if(!(/^1[34578]\d{9}$/.test(this.data.phone))) {
				wx.showModal({
					title: '提示',
					content: "手机号格式有误",
					showCancel: false,
					success: function(res) {

					}
				})
				return false;
			}
		}
		let _this = this;
		api.sendSms({
			data: {
				phone: this.data.phone,
			},
			success: function(res) {
				if(res.data.code == 0) {

				}
			},
		});
		let out = setInterval(function() {
			if(_this.data.countdown == 0) {
				clearInterval(out);
			}
			_this.setTime();
		}, 1000);
	},
	setTime: function(e) {
		let _this = this;

		if(_this.data.countdown == 0) {
			_this.setData({
				smsTrue: false,
				countdown: 60,
				smsName: "获取验证码"
			});
		} else {
			let time = _this.data.countdown;
			time--;
			_this.setData({
				smsTrue: true,
				smsName: time,
				countdown: time
			});
		}

	},
	onLoad: function() {
		//有缓存登录过，直接跳转到活动列表
		wx.getStorage({
		 	key: 'customerInfo',
		  	success: function(res) {
		      	wx.switchTab({
					url: '../cancel/list'
				})
		  	} 
		})
	},
	input_phone: function(e) {
		console.log("@@@@@@@@@@"+e.detail.value)
		this.setData({
			phone: e.detail.value,
		})
	},
	input_checkCode: function(e) {
		this.setData({
			checkCode: e.detail.value,
		})
	},
})